package cn.blbsz.shop.service.impl;

import cn.blbsz.shop.domain.s.UserBaseEntity;
import cn.blbsz.shop.domain.s.UserBaseRepository;
import cn.blbsz.shop.service.UserService;
import cn.blbsz.shop.utils.JdbcUtil;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.hibernate.jpa.HibernateEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2017/3/31.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
	private UserBaseRepository userBaseRepository;

	@PersistenceContext(name = "entityManagerPrimary")
	private EntityManager entityManagerPrimary;

	//处理注册
	@Override
	public String handleRegi(String pid, String tel, String password) {
		 
		StoredProcedureQuery query = entityManagerPrimary.createStoredProcedureQuery("p_user_reg")
				.registerStoredProcedureParameter("pid", Long.class, ParameterMode.IN)
				.registerStoredProcedureParameter("tel", Long.class, ParameterMode.IN)
				.registerStoredProcedureParameter("password", Long.class, ParameterMode.IN)
				.registerStoredProcedureParameter("resultCode", Integer.class, ParameterMode.OUT)
				.registerStoredProcedureParameter("resultMsg", String.class, ParameterMode.OUT)
				.setParameter("pid", Long.valueOf(pid));
		query.execute();
		Integer resultCode = (Integer) query.getOutputParameterValue("resultCode");
		String resultMsg = (String) query.getOutputParameterValue("resultMsg");
		Long userId = Long.valueOf(query.getSingleResult().toString());
		if (1000 == resultCode){
            UserBaseEntity userBaseEntity = new UserBaseEntity(userId, tel, password);
            userBaseRepository.save(userBaseEntity);
		}
		return resultMsg;
	}

}
